﻿Chenxin.UI.TaskDispenseInfo=Chenxin.extend(Chenxin.UI.PageCommon,{
    autoInitForm:false,
    actionUrl:'service/TaskDispenseInfo.ashx',
    createSearch:function(){
		return new Chenxin.UI.TaskDispenseSearch({
			scope:this,
			afterGetParamJson:function(data){
			    data.op='search';
			    data.Type=seltype;	
            }
		});
	},
	createGrid:function(){
		return new Chenxin.UI.Pagination({
			actionUrl:this.actionUrl,
			domId:'page',
			pageSize:15,
			renderTo:'house_table',
			data:{op:"search",Type:seltype},
			render:function(data){
				document.getElementById('house_table').innerHTML=data;
			}
		});
	},
	createForm:function(){
        this.jqmEditMyTaskDispenseForm = new Chenxin.UI.EditMyTaskDispenseForm({
            scope:this,
            afterGetFormJson:function(json){
                delete json.StatusEditMyTask;
            },
            saveSucc:function(data,msg){
                //保存成功后重载Grid
				this.grid.reload();
			}
        });
        this.jqmEditAllTaskDispenseForm = new Chenxin.UI.EditAllTaskDispenseForm({
            scope:this,
            saveSucc:function(data,msg){
                //保存成功后重载Grid
				this.grid.reload();
			},
			afterDelSucc:function(data,msg){
			    //删除成功后重载Grid
				this.grid.reload();
			}
        });
	},
	initEvent:function(){
        var _this=this;

        //事件：任务详情
        $('#btndetail').live('click',function(){
        
            var selectTaskForm;
            if(seltype == "MyTask")//如果点击的是我的任务
            {
                selectTaskForm=_this.jqmEditMyTaskDispenseForm;
            }
            else if(seltype == "AllTask")//如果点击的是全部任务
            {
                selectTaskForm=_this.jqmEditAllTaskDispenseForm;
            }

            if (selectTaskForm.show('detail')) {
                selectTaskForm.setTitle('分配任务');
                 	
                //显示任务详情编辑
                Chenxin.ajax({
				    type: "post",
				    dataType: "json",
				    url: 'service/TaskDispenseInfo.ashx',
				    data: {op:'detail',RecordId:this.name},
				    onSuccess: function(data) {
				        if (data.DealDay != null) data.DealDay = data.DealDay.substr(0,10);
				        data.TaskBeginDay = data.TaskBeginDay.substr(0,10);
				        data.TaskEndDay = data.TaskEndDay.substr(0,10);
    				    
			            if(data.Status==1)
			            {
			                //不可编辑
			                $("#TitleEditAllTask").attr("disabled", true);//主题
			                $("#TaskBeginDayEditAllTask").attr("disabled", true);//预定开始时间
			                $("#TaskEndDayEditAllTask").attr("disabled", true);//预定结束时间       
			                $("#DescriptionEditAllTask").attr("disabled", true);//描述
			                $("#DealDayEditAllTask").attr("disabled", true);//结束时间
			                $("#DealDescriptionEditAllTask").attr("disabled", true);//执行结果
			                $("#TaskType").attr("disabled", true);//类型
			                $("#txtStatus").attr("disabled", true);//状态
    			            
			                //隐藏提示判断Tip
			                $("#TitleEditAllTaskTip").css("display","none");//主题Tip
			                $("#TaskBeginDayEditAllTaskTip").css("display","none");//预定开始时间Tip
			                $("#TaskEndDayEditAllTaskTip").css("display","none");//预定结束时间Tip
			                $("#DescriptionEditAllTaskTip").css("display","none");//描述Tip
			                $("#DealDayEditAllTaskTip").css("display","none");//结束时间Tip
			                $("#DealDescriptionEditAllTaskTip").css("display","none");//执行结果Tip
    			            
			                //隐藏按钮
			                $("#edit_btn2EditAllTask").css("display","none");//编辑按钮
			                $("#del_btn2EditAllTask").css("display","none");//关闭按钮
    			            
			                //隐藏控件
			                $("#TaskTypeEditAllTask").css("display","none");//类型
			                $("#StatusEditAllTask").css("display","none");//状态
    			            
			                //显示控件
			                $("#TaskType").css("display","block");//类型
			                $("#txtStatus").css("display","block");//状态
    			            
			            }
			            else
			            {
			          	    //可编辑
			                $("#TitleEditAllTask").attr("disabled", false);//主题
			                $("#TaskBeginDayEditAllTask").attr("disabled", false);//预定开始时间
			                $("#TaskEndDayEditAllTask").attr("disabled", false);//预定结束时间       
			                $("#DescriptionEditAllTask").attr("disabled", false);//描述
			                $("#DealDayEditAllTask").attr("disabled", false);//结束时间
			                $("#DealDescriptionEditAllTask").attr("disabled", false);//执行结果
			                $("#TaskType").attr("disabled", false);//类型
			                $("#txtStatus").attr("disabled", false);//状态
    			            
			                //显示提示判断Tip
			                $("#TitleEditAllTaskTip").css("display","block");//主题Tip
			                $("#TaskBeginDayEditAllTaskTip").css("display","block");//预定开始时间Tip
			                $("#TaskEndDayEditAllTaskTip").css("display","block");//预定结束时间Tip
			                $("#DescriptionEditAllTaskTip").css("display","block");//描述Tip
			                $("#DealDayEditAllTaskTip").css("display","block");//结束时间Tip
			                $("#DealDescriptionEditAllTaskTip").css("display","block");//执行结果Tip
    			            
			                //显示按钮
			                $("#edit_btn2EditAllTask").css("display","block");//编辑按钮
			                $("#del_btn2EditAllTask").css("display","block");//关闭按钮 
    			            
			                //显示控件
			                $("#TaskTypeEditAllTask").css("display","block");//类型
			                $("#StatusEditAllTask").css("display","block");//状态
    			            
			                //隐藏控件
			                $("#TaskType").css("display","none");//类型
			                $("#txtStatus").css("display","none");//状态 
    			            
			                //如果状态是已处理(1是已处理,0是未处理)
                            if($('#edit_btn2EditAllTask').css("display") == "none")
                            {
                                //显示
                                $('#DealDayEditAllTask').css("display","block");//结束时间
                                $('#DealDayEditAllTaskTip').css("display","block"); 
                                $('#spDealDay').css("display","block");  
                                $('#DealDescriptionEditAllTask').css("display","block");//执行结果
                                $('#DealDescriptionEditAllTaskTip').css("display","block");
                                $('#spDealDescription').css("display","block"); 

                            }
                            else
                            {
                                //隐藏
                                $('#DealDayEditAllTask').css("display","none");//结束时间
                                $('#DealDayEditAllTaskTip').css("display","none"); 
                                $('#spDealDay').css("display","none");  
                                $('#DealDescriptionEditAllTask').css("display","none");//执行结果
                                $('#DealDescriptionEditAllTaskTip').css("display","none");
                                $('#spDealDescription').css("display","none"); 
                                
                                $("#DealDayEditAllTask").val("");//结束时间清空
                                $("#DealDescriptionEditAllTask").val("");//执行结果清空.
                            }
                
			            }
    	
                        //为Form绑定返回数据
			            selectTaskForm.currentModel=data;
				        selectTaskForm.fitData(data);
				    }

			    });	
            }
            
		});
		
		//分配任务        
        $("#aalltask").click(function() {
            seltype ="AllTask";
            var param = {Type:"AllTask"};		
            _this.grid.reload(param,true);//重载Grid
        });

        //我的任务
        $("#amytask").click(function() {
            seltype ="MyTask";
            var param = {Type:"MyTask"};		
            _this.grid.reload(param,true);//重载Grid
        });
        
    },
	initComponent:function(){
		Chenxin.UI.TaskDispenseInfo.superclass.initComponent.call(this);
	}
}); 


/**
*我的任务详情编辑
*/
Chenxin.UI.EditMyTaskDispenseForm=Chenxin.extend(Chenxin.UI.FormCommon,{	
	actionUrl:'/service/TaskDispenseInfo.ashx',
	op:''+seltype+'',
	id:'jqmEditMyTaskDispense',
	renderTo:'divMyTaskDispenseInfo',
	jqmWidth:700,
	createComboList:function(){
	    //任务类型
	    var cmbtaskstatusType = new Chenxin.UI.Combo({
            containerId:'#StatusEditMyTask',
            name:"Status",
            tbxWidth:110,
            required:true,
            inModelType:'val',
            items:comboCommon.taskstatusType
        });	
		return [cmbtaskstatusType];
	},
	initFormValidator:function(){
	    var _this=this;
	    //结束时间验证
	    $("#DealDayEditMyTask").focus(function(){
            WdatePicker({oncleared:function(){$(this).blur();},onpicked:function(){$(this).blur();}})
        }).formValidator({validatorgroup:this.id,
            onshow:"请输入结束时间!",
            onfocus:"请输入结束时间!",
            oncorrect:" "
        }).inputValidator({
            empty: { leftempty: false, rightempty: false, emptyerror: "结束时间不能为空!" }, 
            type:"date",
            onerror:"结束时间不能为空!"
        });
        //执行情况描述验证
        $("#DealDescriptionEditMyTask").formValidator({ validatorgroup:this.id,
            onshow: "请输入执行情况描述!", onfocus: "请输入执行情况描述!", oncorrect: " "
        }).inputValidator({
            min: 1, onerror: "执行情况描述不能为空!"
        }).functionValidator({
            fun:function(val,elem){
                if(val.replace(/^\ +|\ +$/ig,"") != ""){
                    return true;
                }else{
                    return "执行情况描述不能为空!"
                }
            }, onerror: "执行情况描述不能为空!"
        }); 
	},
	createForm:function(){
		return formCommon.EditMyTaskDispenseForm('EditMyTask');
	},
	initEvent:function(){

	    var _this=this;
	    //编辑事件
		$('#edit_btnEditMyTask').click(function(){
            _this.op="update";
            _this.save();   
		});
		//关闭事件
		$('#close_btnEditMyTask').click(function(){
			_this.hide();
		});	
		
	},
	initComponent:function(){
		Chenxin.UI.EditMyTaskDispenseForm.superclass.initComponent.call(this);
	}
});

/**
*分配的任务详情编辑
*/
Chenxin.UI.EditAllTaskDispenseForm=Chenxin.extend(Chenxin.UI.FormCommon,{	
	actionUrl:'/service/TaskDispenseInfo.ashx',
	op:''+seltype+'',
	id:'jqmEditAllTaskDispense',
	renderTo:'divAllTaskDispenseInfo',
	jqmWidth:700,
	createComboList:function(){
	    var _this=this;
	    //任务状态
	    var cmbtaskstatusType = new Chenxin.UI.Combo({
            containerId:'#StatusEditAllTask',
            name:"Status",
            tbxWidth:110,
            required:true,
            inModelType:'val',
            items:comboCommon.taskstatusType
        });	
        //任务类型
		var cmbtaskType=new Chenxin.UI.ParameterCombo({
			containerId:'#TaskTypeEditAllTask',
			key:'Task.Type',
			autoLoad:true,
			tbxWidth:110,
			paramType:'text',
			inModelType:'txt',
			name:'TaskType',
			autoLoad:true,
			required:true			
		});	
		return [cmbtaskstatusType,cmbtaskType];
	},
	initFormValidator:function(){
	    var _this=this;
	    
	    //主题验证
	    $("#TitleEditAllTask").formValidator({ validatorgroup:this.id,
            onshow: "请输入主题!", onfocus: "请输入主题!", oncorrect: " "
        }).inputValidator({
            min: 1, empty: { leftempty: false, rightempty: false, emptyerror: "主题不能为空!" }, onerror: "主题不能为空!"
	    });
	    
	    //主题预定开始时间
		$("#TaskBeginDayEditAllTask").focus(function(){
            WdatePicker({oncleared:function(){$(this).blur();},onpicked:function(){$(this).blur();}})
        }).formValidator({validatorgroup:this.id,
            onshow:"请输入预定开始日期!",
            onfocus:"请输入预定开始日期!",
            oncorrect:" "
        }).inputValidator({
            empty: { leftempty: false, rightempty: false, emptyerror: "预定开始日期不能为空!" }, 
            type:"date",
            onerror:"预定开始日期不能为空!"
        });
        
		//主题预定结束时间
		$("#TaskEndDayEditAllTask").focus(function(){
            WdatePicker({oncleared:function(){$(this).blur();},onpicked:function(){$(this).blur();}})
        }).formValidator({validatorgroup:this.id,
            onshow:"请输入预定结束日期!",
            onfocus:"请输入预定结束日期!",
            oncorrect:" "
        }).inputValidator({
            empty: { leftempty: false, rightempty: false, emptyerror: "预定结束日期不能为空!" }, 
            type:"date",
            onerror:"预定结束日期不能为空!"
        }).functionValidator({
	        fun: function(val, elem) {
	            if ($("#TaskBeginDayEditAllTask").val() != "" && $("#TaskEndDayEditAllTask").val() != "") {
	                var d1 = new Date(Date.parse($("#TaskBeginDayEditAllTask").val().replace(/-/g, "/")));
	                var d2 = new Date(Date.parse($("#TaskEndDayEditAllTask").val().replace(/-/g, "/")));
	                if ((d1.getTime() - d2.getTime()) > 0) {
	                    return false;
	                } else {
	                    return true;
	                }
	            }
	        }, onerror: "预定开始时间大于预定结束时间!"
	    });
	    //结束时间验证
	    $("#DealDayEditAllTask").focus(function(){
            WdatePicker({oncleared:function(){$(this).blur();},onpicked:function(){$(this).blur();}})
        }).formValidator({validatorgroup:this.id,
            onshow:"请输入结束时间!",
            onfocus:"请输入结束时间!",
            oncorrect:" "
        }).inputValidator({
            empty: { leftempty: false, rightempty: false, emptyerror: "结束时间不能为空!" }, 
            type:"date",
            onerror:"结束时间不能为空!"
        });
        //执行结果验证
        $("#DealDescriptionEditAllTask").formValidator({ validatorgroup:this.id,
            onshow: "请输入执行结果!", onfocus: "请输入执行结果!", oncorrect: " "
        }).inputValidator({
            min: 1, onerror: "执行结果不能为空!"
        }).functionValidator({
            fun:function(val,elem){
                if(val.replace(/^\ +|\ +$/ig,"") != ""){
                    return true;
                }else{
                    return "执行结果不能为空!"
                }
            }, onerror: "执行结果不能为空!"
        });
	},
	createForm:function(){
		return formCommon.EditAllTaskDispenseForm('EditAllTask');
	},
	initEvent:function(){
	    var _this=this;
	    //编辑事件
		$('#edit_btn2EditAllTask').click(function(){
            _this.op="update";
            _this.save();    
		});
		//关闭事件
		$('#close_btn2EditAllTask').click(function(){
			_this.hide();
		});	
		//删除事件
		$('#del_btn2EditAllTask').live('click',function(){
		    _this.del($("#hidRecordId2").val(),"del");
		});
		
		//Selected事件
		$('#StatusEditAllTask').click(function(){

		    //如果状态是已处理(1是已处理,0是未处理)
            if(_this.comboList[0].getValue() == 1)
            {
                //显示
                $('#DealDayEditAllTask').css("display","block");//结束时间
                $('#DealDayEditAllTaskTip').css("display","block"); 
                $('#spDealDay').css("display","block");  
                $('#DealDescriptionEditAllTask').css("display","block");//执行结果
                $('#DealDescriptionEditAllTaskTip').css("display","block");
                $('#spDealDescription').css("display","block");

            }
            else
            {
                //隐藏
                $('#DealDayEditAllTask').css("display","none");//结束时间
                $('#DealDayEditAllTaskTip').css("display","none"); 
                $('#spDealDay').css("display","none");  
                $('#DealDescriptionEditAllTask').css("display","none");//执行结果
                $('#DealDescriptionEditAllTaskTip').css("display","none");
                $('#spDealDescription').css("display","none"); 
                
                $("#DealDayEditAllTask").val("");//结束时间清空
                $("#DealDescriptionEditAllTask").val("");//执行结果清空.

            }
            
		});
		
	},
	formValidator: function(model) {
       var _this = this;
       var status = true;

       switch (model.Status) {
            case '1':
                $("#DealDayEditAllTask").unFormValidator(false);
                $("#DealDescriptionEditAllTask").unFormValidator(false);
                status = jQuery.formValidator.pageIsValid(this.id) && this.checkCombo();
                break;
            case '0':
                $("#DealDayEditAllTask").unFormValidator(true);
                $("#DealDescriptionEditAllTask").unFormValidator(true);
                status = jQuery.formValidator.pageIsValid(this.id);
                break;               
            default:
                $("#DealDayEditAllTask").unFormValidator(true);
                $("#DealDescriptionEditAllTask").unFormValidator(true);
                status = jQuery.formValidator.pageIsValid(this.id) && this.checkCombo();
                break;

        } 
        
        if (!status) {
            alert('请填写完必填(选)项再进行表单提交！');
        }
        return status;
        
    },
	initComponent:function(){
		Chenxin.UI.EditAllTaskDispenseForm.superclass.initComponent.call(this);
	}
});


/**
*查询面板
*/
Chenxin.UI.TaskDispenseSearch=Chenxin.extend(Chenxin.UI.SearchCommon,{
    id:'house_search',
	initEvent:function(){
		var _this=this; 
        
		//查询按钮
		$('#btnSreachTaskDispenseInfo').click(function(){
		    _this.scope.reloadGrid();
		});
        
	},
	createComboList:function(){
	    //接收人部门
		var cmbDepartment = new Chenxin.UI.StructCombo({
            param: { op: 'getcontroldeparts' },
            containerId: '#divSearchDepartment',
            inModelType: 'all',
            name:'divSearchDepartmentId',
			txtModelName:"ReceiverDepart",
			tbxWidth:45,
            onSelected: function() {
				if(this.getValue()==this.defVal){
					cmbTaskUser.clearItems();
				}else{
				    cmbTaskUser.param.fatherid=this.getValue();
				    cmbTaskUser.reload();
				}
            }
        });
        //接收人
        var cmbTaskUser = new Chenxin.UI.StructCombo({
            autoLoad: false,
            param: { op: 'getemployee'},
            inModelType: 'all',
            name:'divSearchTaskUserId',
			txtModelName:"taskReceiver",
			tbxWidth:45,
            containerId: '#divSearchTaskUser'
        });
        //状态
		var cmbTaskStatus = new Chenxin.UI.Combo({
			containerId: '#divSearchTaskStatus',
			tbxWidth:45,
			items:comboCommon.taskstatusType
		});
        
		return [cmbDepartment,cmbTaskUser,cmbTaskStatus];
	},
    initComponent: function() {
        Chenxin.UI.TaskDispenseSearch.superclass.initComponent.call(this);
    }
});

//初始定义一个全局变量
//MyTask---我的任务tab
//AllTask---分配任务tab
var seltype = "MyTask";
                 	
$(document).ready(function(){
    $('#container-1 > ul').tabs();
    this.TaskDispenseInfo=new Chenxin.UI.TaskDispenseInfo();
});